package com.b2.controller.web;

import com.b2.pojo.dto.MergeDto;
import com.b2.pojo.entity.Video;
import com.b2.result.Result;
import com.b2.service.VideoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/web/Video")
@Slf4j
@Api(tags = "视频资源相关接口")
@CrossOrigin
public class VideoController {

    // 注入VideoService接口对象
    @Resource
    private VideoService videoService;

    @PostMapping("/list")
    @ApiOperation("根据课程id查询视频")
    public Result<List<Video>> pageList(@ApiParam("课程id") @RequestParam(name = "courseId") Long courseId){
        return videoService.listAll(courseId);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除视频")
    public Result<String> deleteVideo(@ApiParam("视频id") @RequestParam(name = "videoId") Long videoId){
        videoService.delete(videoId);
        return Result.success();
    }

    @PostMapping("update")
    @ApiOperation("修改视频信息")
    public Result<String> update(@RequestBody Video video) {
        videoService.updateById(video);
        return Result.success();
    }

    @ApiOperation(value = "文件上传前检查文件")
    @PostMapping("/upload/checkFile")
    public Result<Boolean> checkFile(@RequestParam("fileMd5") @ApiParam("视频文件的Md5") String fileMd5,
                                     @RequestParam("fileName") @ApiParam("视频文件名") String fileName) throws Exception {
        return videoService.checkFile(fileMd5, fileName);
    }


//    @ApiOperation(value = "分块文件上传前的检测")
//    @PostMapping("/upload/checkChunk")
//    public Result<Boolean> checkChunk(@RequestBody MultipartUploadDto multipartUploadDto) throws Exception {
//        return videoService.checkChunk(multipartUploadDto);
//    }

    @ApiOperation(value = "获取uploadId")
    @PostMapping("/upload/uploadId")
    public Result<String> uploadId(@RequestParam("fileMd5") @ApiParam("视频文件的Md5") String fileMd5,
                                   @RequestParam("fileName") @ApiParam("视频文件名") String fileName){
        return videoService.uploadId(fileMd5, fileName);
    }

    @ApiOperation(value = "上传分块文件")
    @PostMapping("/upload/uploadChunk")
    public Result<String> uploadChunk(@RequestParam("file") @ApiParam("当前分块文件") MultipartFile file,
                                        @RequestParam("fileMd5") @ApiParam("视频文件的Md5") String fileMd5,
                                        @RequestParam("fileName") @ApiParam("视频文件名") String fileName,
                                        @RequestParam("chunkIndex") @ApiParam("当前分块文件的片数") int chunkIndex,
                                        @RequestParam("uploadId") @ApiParam("分块文件的uploadId") String uploadId) throws Exception {
          return videoService.uploadChunk(file, fileMd5, fileName, chunkIndex, uploadId);
    }

    @ApiOperation(value = "保存并合并文件")
    @PostMapping("/upload/mergeChunks")
    public Result<String> mergeChunks(@RequestBody MergeDto mergeDto) throws Exception {
       return videoService.mergeChunks(mergeDto);
    }
}
